package com.java.laixiangdong.library.bean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author laixiangdong
 * 图书类
 */
public class Book implements Comparable, Serializable {
    private String name; // 名称
    private String info; // 简介
    private Date publishTime; // 出版日期
    private int price; // 价格
    private static int sortFlag = 1; // 用来控制排序规则

    public Book() {
    }

    public Book(String name, String info, Date publishTime, int price) {
        this.name = name;
        this.info = info;
        this.publishTime = publishTime;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public static void setSortFlag(int sortFlag) {
        if (sortFlag < 4 && sortFlag > 0) {
            Book.sortFlag = sortFlag;
        } else {
            System.out.println("设置失败，排序方式未更改");
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return getName().equals(book.getName());

    }

    @Override
    public int hashCode() {
        return Objects.hash(getName());
    }

    @Override
    public String toString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return "书名：" + name + '\t' + "简介：" + info + '\t' + "出版时间：" + '\t' + dateFormat.format(publishTime) + "\t" + "价格：" + price;
    }

    @Override
    public int compareTo(Object o) {
        if (o == null || getClass() != o.getClass()) return 0;
        else {
            switch (sortFlag) {
                case 1:
                    Book b1 = (Book) o;
                    if (b1.getPrice() > this.price)
                        return 1;
                    else if (b1.getPrice() < this.price)
                        return -1;
                    else
                        return 0;
                case 2:
                    Book b2 = (Book) o;
                    if (b2.getPrice() > this.price)
                        return -1;
                    else if (b2.getPrice() < this.price)
                        return 1;
                    else
                        return 0;
                default:
                    Book b3 = (Book) o;
                    if (b3.getPublishTime().getTime() > this.publishTime.getTime())
                        return -1;
                    else if (b3.getPublishTime().getTime() < this.publishTime.getTime())
                        return 1;
                    else
                        return 0;
            }
        }
    }
}
